<template>
  <div class="news-list-layout">
    <homeHeader v-if="clientwidth>1080"></homeHeader>
    <header-phone v-else></header-phone>
    <div class="news-header" :style="{backgroundImage:'url('+haderBg+')' }">
      <div class="mask"></div>
      <p class="title webfont-Medium">
        关注互联网业界动态，抓住营销增长新趋势
      </p>
      <p class="sub-title">不能错过每一个实现用户增长的机会！</p>
    </div>

    <div class="news-list-main flex">

      <div class="news-box">
        <div class="news-title">
          <p class="title webfont-Medium">{{ news.title }}</p>
          <div class="new-sec flex">
            <p>
              <i class="el-icon-date"></i>
              <span>{{ news.time }}</span>
            </p>

          </div>
        </div>
        <div class="news-main" style="width: 832px;padding-top: 24px;">
          <div style="width: 750px;margin: 0 auto;" v-html="content"></div>
        </div>

      </div>

      <div class="main-slide">
        <p class="slide-title webfont-Medium">{{ newsList.length?newsList[0].name:'' }}</p>
        <ul v-if="newsList.length&&clientwidth>1080">
          <template v-for="(item,index) in newsList[0].list">
            <li v-if="index<5" :key="item.time" class="slide-item flex">
              <div class="item-img">
                <div class="new-img pointer" @click="toDetails(item.media_id)">
                  <el-image v-if="item.thumb_url" lazy fit="cover" :src="item.thumb_url" class="img-top">
                    <div slot="error" class="image-slot">
                      <i class="el-icon-picture-outline" />
                    </div>
                  </el-image>
                  <el-image v-if="item.thumb_url" lazy fit="cover" :src="item.thumb_url" class="img-bg">
                    <div slot="error" class="image-slot">
                      <i class="el-icon-picture-outline" />
                    </div>
                  </el-image>
                </div>
              </div>
              <div class="new-info " @click="toDetails(item.media_id)">
                <div class="news-title webfont-Medium overflow-ellipsis pointer" :title="item.title">
                  {{ item.title }}
                </div>
                <div class="news-digest  overflow-ellipsis pointer">{{ item.digest }} </div>
              </div>
            </li>
          </template>

        </ul>

        <div v-if="newsList.length&&clientwidth<=1080" class="ul-box">
          <ul v-if="newsList.length" :style="{width:(newsList[0].list.length+1)*17+'rem' }">
            <template v-for="(item,index) in newsList[0].list">
              <li :key="item.time" class="slide-item">
                <div class="new-type" :style="{color: spliceColor(item.typeLabel)}">
                  {{ spliceName(item.typeLabel) }}
                </div>
                <div class="new-main">
                  <div class="new-index">{{ index<9?'0'+(index+1):index }}</div>

                  <div class="new-info " @click="toDetails(item.media_id)">
                    <div class="news-title webfont-Medium overflow-ellipsis pointer" :title="item.title">
                      {{ item.title }}
                    </div>
                    <div class="news-digest  overflow-ellipsis pointer">{{ item.digest }} </div>
                  </div>

                  <div class="item-img">
                    <div class="new-img pointer" @click="toDetails(item.media_id)">
                      <el-image v-if="item.thumb_url" lazy fit="cover" :src="item.thumb_url" class="img-top">
                        <div slot="error" class="image-slot">
                          <i class="el-icon-picture-outline" />
                        </div>
                      </el-image>

                    </div>
                  </div>
                </div>
                <div class="new-date">
                  <i class="el-icon-date"></i>
                  <span>{{ item.time }}</span>
                </div>
              </li>
            </template>

          </ul>
        </div>
      </div>
    </div>

    <home-footer v-if="clientwidth>1080" style="margin-top: 24px;"></home-footer>

  </div>
</template>
<script>
import {
  findBatchgetMaterial,
  findMaterial
} from '@/api/user.js'
import {
  getTime
} from '@/utils/moment.js'

import homeHeader from '@/components/home/header.vue'
import headerPhone from '@/components/home/header-phone.vue'
import homeFooter from '@/components/home/footer.vue'
export default {
  name: 'NewsDetails', // 路由名称
  components: {
    homeHeader,
    homeFooter,
    headerPhone
  },
  data() {
    return {
      clientwidth: window.innerWidth,
      haderBg: require('@/assets/images/homepage/news-header-bg.svg'),
      news: {},
      content: '',
      newsList: []
    }
  },
  computed: {

  },
  watch: {
    // obj: { //监听的对象
    //   deep: true, //深度监听设置为 true
    //   // 代表在wacth里声明了firstName这个方法之后立即先去执行handler方法
    //       immediate: true
    //   handler: function(newV, oldV) {
    //     console.log('watch中：', newV)
    //   }
    // }
  },
  // 创建后
  created() {
    this.getNewsDetails(this.$route.query.mid)
    this.findWXNewsList()
  },
  // 挂载后
  mounted() {
    window.addEventListener('resize', () => {
      this.clientwidth = window.innerWidth
    })
    // <meta name="referrer" content="never">
    // const metaInfo = document.createElement('meta')
    // metaInfo.setAttribute('name',
    //   'referrer')
    // metaInfo.setAttribute('content',
    //   'never')
    // metaInfo.src = 'https://hm.baidu.com/hm.js?d9040b2f7c03f685cb929f478aa32720'
    // document.head.prepend(metaInfo)
  },
  // 销毁前
  beforeDestroy() {
    window.removeEventListener('resize', () => {})
    // const metaList = document.getElementsByName('referrer')
    // metaList.forEach(item => {
    //   if (item.content === 'never') {
    //     document.head.removeChild(
    //       item)
    //   }
    // })
  },

  // 方法
  methods: {
    spliceColor(name) {
      let index = this.newsList.findIndex(item => {
        return item.name === name
      })
      index = index < 0 ? 0 : index - 1
      const arr = ['rgb(184, 123, 185)', 'rgb(97, 175, 200)', 'rgb(221, 195, 128)', 'rgb(162, 139, 228)',
        'rgb(221, 128, 128)', 'rgb(61, 83, 221)'
      ]
      return arr[index]
    },
    spliceName(name) {
      if (name !== '半月时谈' && name !== '增长日历' && name !== '推广干货') {
        return name.slice(0, 2)
      } else {
        return name.slice(2)
      }
    },

    // 获取微信文章详情；
    async getNewsDetails(mediaId) {
      const obj = {
        mediaId
      }
      const res = await findMaterial(obj)
      this.news = res.data.data.news_item[0]
      const that = this

      this.news.time = getTime({
        time: res.data.data.update_time * 1000,
        type: 'YYYY年MM月DD日HH:mm:ss'
      })
      this.content = res.data.data.news_item[0].content
      this.$nextTick(() => {
        var imgs = document.querySelectorAll('.news-main img')
        for (var i = 0; i < 2; i++) {
          imgs[i].src = imgs[i].getAttribute('data-src')
          imgs[i].style.maxWidth = '100% !important'
        }
        // offsetTop是元素与offsetParent的距离，循环获取直到页面顶部
        function getTop(e) {
          var T = e.offsetTop
          while (e = e.offsetParent) {
            T += e.offsetTop
          }
          return T
        }

        function lazyLoad(imgs) {
          var H = document.documentElement.clientHeight // 获取可视区域高度
          var S = document.documentElement.scrollTop || document.body.scrollTop
          for (var i = 0; i < imgs.length; i++) {
            if (H + S > getTop(imgs[i])) {
              imgs[i].src = imgs[i].getAttribute('data-src')
              imgs[i].style.maxWidth = '100% !important'
            }
          }
        }

        window.onload = window.onscroll = function() { // onscroll()在滚动条滚动的时候触发
          lazyLoad(imgs)
        }

        return
      })
    },

    // 获取微信文章列表
    async findWXNewsList() {
      const res = await findBatchgetMaterial()
      if (res.data.code === 20000) {
        // const arr = res.data.data.item
        const arr = [{
          name: '最新动态',
          list: []
        }]

        const result = res.data.data

        for (const key in result) {
          const index = arr.findIndex(item => {
            return key === item.name
          })

          const list = result[key].map(v => {
            const obj = {
              typeLabel: key,
              time: getTime({
                time: v.update_time * 1000,
                type: 'YYYY年MM月DD日HH:mm:ss'
              }),
              media_id: v.media_id,
              ...v.content.news_item[0]
            }
            return obj
          })
          arr[0].list.push(...list)
          if (index === -1) {
            arr.push({
              name: key,
              list
            })
          }
        }

        arr[0].list = arr[0].list.sort((a, b) => {
          return a.update_time - b.update_time
        })
        this.newsList = arr
        console.log(this.newsList[0], '你好')
        this.loadingtext = '没有更多了'
        return
      } else {
        this.loadingtext = '加载失败，请刷新重试'
      }
    },
    toDetails(mid) {
      const type = this.$route.query.type
      this.$router.push({
        path: '/news-details',
        query: {
          mid,
          type

        }
      })
      this.getNewsDetails(mid)
    }
  }
}
</script>

<style scoped lang="scss">
  @import '~@/styles/variables.scss';

  body section img {
    max-width: 100% !important;
    box-sizing: border-box !important;
    -webkit-box-sizing: border-box !important;
    word-wrap: break-word !important;
  }

  .news-list-layout {
    background-color: #f9f9f9 !important;

    .news-header {
      padding-top: 270px;
      width: 100%;
      min-width: 1200px;
      height: 572px;
      margin-bottom: 24px;
      overflow: hidden;
      position: relative;
      background-repeat: no-repeat;
      background-size: cover;

      .mask {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 0;
        background-color: rgba($color: #000000, $alpha: 0.5);

      }

      p {
        position: relative;
        z-index: 1;
        margin: 0 auto;
        max-width: 1200px;
        text-align: left;
        color: #FFFFFF;
      }

      .title {
        font-size: 46px;
        line-height: 64px;
        margin-bottom: 48px;
      }

      .sub-title {
        font-size: 24px;
        line-height: 34px;
      }
    }

    .news-list-main {
      margin: 0 auto;
      width: 1200px;
      min-width: 1200px;
      overflow: hidden;
      align-items: flex-start;

      .main-slide {
        width: 344px;
        margin-left: 24px;
        min-height: 80vh;
        background-color: #FFFFFF;

        .slide-title {
          height: 44px;
          padding-left: 24px;
          line-height: 44px;
          font-size: 20px;
          background: #FBFBFB;
          border: 1px solid #EEEEEE;
          box-sizing: border-box;
        }

        .slide-item {
          border-bottom: 1px solid #EEEEEE;
          padding: 24px 22px 20px 25px;

          &:last-of-type {
            border: none;
          }

          .item-img {
            margin-right: 12px;
            position: relative;
            width: 105px;
            height: 45px;

            .img-top,
            .img-bg {
              width: 105px;
              height: 45px;
            }

            .img-top {
              position: relative;
              z-index: 2;

            }

            .img-bg {
              position: absolute;
              z-index: 1;
              top: 4px;
              left: -4px;
              opacity: 0.2;

            }
          }

          .new-info {
            .news-title {
              font-size: 14px;
              line-height: 14px;
              margin-bottom: 12px;
              max-width: 166px;
            }

            .news-digest {
              font-size: 12px;
              line-height: 12px;
              max-width: 176px;
            }
          }

        }

      }

      .news-box {
        // max-height: 80vh;
        padding-bottom: 24px;
        overflow: hidden;
        background-color: #FFFFFF;
        // overflow-y: scroll;

        .news-title {
          padding: 24px 40px;
          background: #FBFBFB;
          border: 1px solid #EEEEEE;

          .title {
            color: #39444E;
            font-size: 20px;
            line-height: 20px;
            margin-bottom: 26px;
          }

          .new-sec {
            justify-content: flex-start;

            p {
              margin-right: 24px;
              font-size: 12px;
              color: #ACB6C2;

              i {
                margin-right: 8px;
                font-size: 14px;
                color: $mainColor1;

              }
            }
          }

          .news-main {
            width: 832px;
          }

        }

      }

    }

  }

  .news-box::-webkit-scrollbar {
    width: 10px;
    height: 20px;
    background-color: #F5F5F5;
  }

  .news-box::-webkit-scrollbar-thumb {
    background-color: #e5e5e5;
    border-radius: 10px;
  }

  /* pc端小屏样式*/
  @media screen and (min-width: 1081px) and (max-width: 1240px) {
    .news-list-layout {
      .news-header {
        min-width: 100vw;
        max-width: 1200px;

        p {
          text-align: center;
        }
      }

      .news-list-main {
        max-width: 1200px;
        min-width: 100vw;
        justify-content: center;
      }

      .main-slide {
        display: none;
      }
    }

  }

  @media screen and (max-width: 1080px) {
    .news-list-layout {
      width: 100vw;
      min-width: 100vw;

      .news-header {
        width: 100vw;
        min-width: 100vw;
        background-position: center;
        height: 20.457rem;
        overflow: hidden;
        padding: 0 1.714rem;
        padding-top: 8.26rem;
        margin-bottom: 0;

        .title {
          font-weight: 600;
          font-size: 1.371rem;
          line-height: 2.286rem;
          width: 100%;
          text-align: center;
        }

        .sub-title {
          width: 100%;
          text-align: center;
          font-size: 0.8rem;
          line-height: 0.8rem;
        }
      }

      .news-list-main {
        display: block;
        min-width: 100vw;
        width: 100vw;

        .news-box {
          .news-title {
            padding: 1.6rem;
            font-weight: 500;
            font-size: 1rem;
            line-height: 1rem;
            background-color: #FFFFFF;
            border: none;
          }
        }

        .news-main {
          width: 100% !important;
          padding: 0 1.6rem;
          padding-top: 0 !important;

          >div {
            width: 100% !important;
          }
        }

        .main-slide {
          width: 100vw;
          margin: 0;
          min-height: auto;
          height: auto;

          .slide-title {
            height: auto;
            padding: 14px 1.6rem;
            font-weight: 500;
            font-size: 0.686rem;
            line-height: 0.971rem;
            color: rgba(172, 182, 194, 1);
            background: transparent;
            border: none;
          }

          .ul-box {
            padding-bottom: 2.734rem;
            margin-left: 1.6rem;
            width: 100%;
            overflow-y: hidden;
            overflow-x: auto;
            scrollbar-width: none;
            -ms-overflow-style: none;

            &::-webkit-scrollbar {
              display: none;
              /* Chrome Safari */
            }

          }

          ul {
            width: 400vw;
            display: flex;

            .slide-item {
              width: 16.514rem;
              padding: 0;
              padding-top: 1.37rem;
              padding-bottom: 0.857rem;
              margin-right: 0.9rem;
              border-bottom: none !important;
              box-shadow: 0.571rem 0.571rem 0.686rem rgba(0, 0, 0, 0.04);

              .new-type {
                margin-bottom: 0.5rem;
                font-weight: 500;
                font-size: 0.686rem;
                line-height: 0.971rem;
                text-indent: 2rem;
              }

              .new-main {
                display: flex;
                align-items: flex-start;
                justify-content: space-between;
                padding-left: 0.86rem;
                padding-right: 1.37rem;
                overflow: hidden;

                .new-index {
                  color: rgba(172, 182, 194, 1);
                  font-size: 0.8rem;
                  line-height: 1.029rem;
                }

                .new-info {
                  max-width: 9.771rem;

                  .news-title {
                    font-size: 0.8rem;
                    line-height: 0.9rem;
                  }

                  .news-digest {
                    height: 2.2rem;
                    max-width: 9rem;
                    font-size: 0.686rem;
                    line-height: 1.029rem;
                  }
                }

                .item-img {
                  width: 2.457rem !important;
                  height: 2.343rem !important;
                  margin: 0;

                  .img-top {
                    width: 2.457rem !important;
                    height: 2.343rem !important;
                  }
                }
              }

              .new-date {
                padding-top: 0.3rem;
                padding-left: 2rem;
                display: flex;
                align-items: center;
                justify-content: flex-start;
                i {
                  font-size: 0.8rem;
                  margin-right: 10px;
                  color: rgba(21, 197, 206, 1);
                }

                span {
                  font-family: PingFang SC;
                  font-size: 0.686rem;
                  line-height: 0.686rem;
                }
              }
            }
          }

        }
      }
    }

  }
</style>
